package net.wsfcp.android.http.mvp.model;

import android.support.annotation.NonNull;

import com.google.gson.Gson;

import net.wsfcp.android.BuildConfig;
import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.base.BaseModel;
import net.wsfcp.android.bean.requestbean.ReqMainTabBean;
import net.wsfcp.android.bean.resultbean.TabBean;
import net.wsfcp.android.http.exception.ApiException;
import net.wsfcp.android.http.exception.ExceptionEngine;
import net.wsfcp.android.http.exception.ServerException;
import net.wsfcp.android.http.mvp.subscriber.CommonSubscriber;
import net.wsfcp.android.http.mvp.subscriber.ErrorSubscriber;
import net.wsfcp.android.http.mvp.transformer.CommonTransformer;
import net.wsfcp.android.http.UrlHelper;

import java.util.ArrayList;

import okhttp3.MediaType;
import okhttp3.RequestBody;

//侧边栏菜单
public class MainModel extends BaseModel {

    public void getTab(@NonNull final MainInfoHint infoHint) {
        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        ReqMainTabBean bean = new ReqMainTabBean(UrlHelper.CLIENT_TYPE, UrlHelper.getUserName());
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.getTabBar(body).compose(new CommonTransformer()).subscribe(new CommonSubscriber<ArrayList<TabBean>>(BaseApplication.getContext()) {
            @Override
            public void accept(ArrayList<TabBean> tabBean) {
                infoHint.getTabSuccess(tabBean);
            }

        }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
            @Override
            public void accept(Throwable t) {
                ServerException e = ExceptionEngine.handleException(t);
                    infoHint.getTabFail( e.message);

            }

        });

    }


    public interface MainInfoHint {
        //获取侧边栏菜单回调
        void getTabSuccess(ArrayList<TabBean> tabBean);

        void getTabFail(String str);


    }

}
